<template>
  <div class="backend-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-left">
        <el-button 
          link
          class="collapse-btn"
          @click="isCollapse = !isCollapse"
        >
          <el-icon><Fold v-if="!isCollapse"/><Expand v-else/></el-icon>
        </el-button>
        <h1>猫眼电影数据分析系统</h1>
      </div>
      <div class="header-right">
        <el-dropdown trigger="click">
          <div class="user-info">
            <el-avatar :size="32" icon="UserFilled" />
            <span class="username">{{ currentUser?.name }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="showUserDetail">
                <el-icon><User /></el-icon>个人信息
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主要内容区 -->
    <div class="main-container">
      <!-- 使用Sidebar组件 -->
      <Sidebar :is-collapse="isCollapse" />

      <!-- 内容区 -->
      <div class="content" :class="{ 'content-collapse': isCollapse }">
        <div class="page-content">
          <router-view></router-view>
        </div>
      </div>
    </div>
    <UserDetailDialog ref="userDetailDialogRef" />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userApi } from '../api/user'
import UserDetailDialog from '../components/UserDetailDialog.vue'
import Sidebar from '../components/Sidebar.vue'
import {
  Fold,
  Expand,
  User,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const userDetailDialogRef = ref(null)
const currentUser = ref(null)
const isCollapse = ref(false)

onMounted(() => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    currentUser.value = JSON.parse(userStr)
  }
})

const userRole = computed(() => {
  if (currentUser.value?.is_superuser && currentUser.value?.is_staff && currentUser.value?.is_admin) {
    return '超级管理员'
  }
  if (currentUser.value?.is_admin) {
    return '管理员'
  }
  return '普通用户'
})

const handleLogout = async () => {
  try {
    const refreshToken = localStorage.getItem('refresh_token')
    await userApi.logout({ refresh_token: refreshToken })
    localStorage.clear()
    ElMessage.success('退出成功')
    await router.push('/login')
  } catch (error) {
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else {
      ElMessage.error('退出失败')
    }
  }
}

const showUserDetail = () => {
  userDetailDialogRef.value.showDialog()
}
</script>

<style scoped>
.backend-container {
  width: 100vw;
  height: 100vh;
  background: #f0f2f5;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

.header {
  height: 60px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.header-left h1 {
  font-size: 18px;
  margin: 0;
  color: #303133;
}

.collapse-btn {
  padding: 0;
  font-size: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.content {
  flex: 1;
  padding: 16px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  position: relative;
}

.content-collapse {
  margin-left: 0;
}

.page-content {
  background: #fff;
  padding: 16px;
  border-radius: 4px;
  flex: 1;
  overflow: auto;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: relative;
}
</style>